import { defineStore } from "pinia"
import axios from 'axios'
import { handleError } from '@/plugins/err-handle'
import { useNotificationStore } from '@/stores/components/notification'
import i18n from '@/i18n'

const notificationStore = useNotificationStore()
const { t } = i18n.global

export const useAttachmentStore = defineStore("attachment", {
  state: () => ({
    attachments: [],
    currentDocDetailID: null,
    currentAttachment: {},
  }),
  getters: {
    // doubleCount: (state) => state.counter + 2,
  },
  actions: {
    
    // 添加
    addItem(data) {
      return new Promise((resolve, reject) => {

        axios.post('/admin/attachment/uploadfile', data)
          .then((response) => {
            console.log('attachment addItem: ', response) 
            this.currentAttachment = response.data

            notificationStore.showNotification({
              type: 'success',
              message: t('attachment.upload_suc'),
            })

            resolve(response)
          })
          .catch((err) => {
            handleError(err)
            reject(err)
          })
        })
    },

  },
})